package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.TitanCurrentDialCallback;
import com.apex.bluetooth.callback.TitanWatchFaceListCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.utils.LogUtils;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityDialSystemBinding;
import com.apex.sdk.dialog.TitanWatchFaceDialog;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.List;


public class TitanDialActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    int[] tempList;
    TitanWatchFaceDialog titanWatchFaceDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {

                int faceId = (int) msg.obj;
                if (faceId >= 0) {
                    activityDialSystemBinding.restScreen.setText(faceId + "");
                }
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_dial_list, new TitanWatchFaceListCallback() {
                        @Override
                        public void faceList(List<Integer> dialList) {
                            if (dialList != null && !dialList.isEmpty()) {
                                tempList = new int[dialList.size()];
                                for (int i = 0; i < dialList.size(); i++) {
                                    tempList[i] = dialList.get(i);
                                }
                            }
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x42);
                            }
                        }

                        @Override
                        public void mutualFail(int errorCode) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x42);
                            }
                        }
                    });
                } else {
                    if (waitingDialog != null) {
                        if (waitingDialog.isShowing()) {
                            waitingDialog.dismiss();
                        }
                    }
                }
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(TitanDialActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x45) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(TitanDialActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityDialSystemBinding activityDialSystemBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDialSystemBinding=ActivityDialSystemBinding.inflate(getLayoutInflater());
        setContentView(activityDialSystemBinding.getRoot());

        activityDialSystemBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDialSystemBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDialSystemBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityDialSystemBinding.systemLayout.setVisibility(View.GONE);
        activityDialSystemBinding.dialID.setText(getString(R.string.dial_ID));
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(TitanDialActivity.this);
            }
            waitingDialog.show();
            LogUtils.e(TAG, "开始查询当前表盘");
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_current_dial, new TitanCurrentDialCallback() {
                @Override
                public void dialId(int watchFace) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = watchFace;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });

        }
        activityDialSystemBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 if (dList == null || dList.isEmpty()) {
                 Toast.makeText(TitanDialActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
                 return;
                 }
                 */
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (titanWatchFaceDialog == null) {
                        titanWatchFaceDialog = new TitanWatchFaceDialog(TitanDialActivity.this, tempList);
                        titanWatchFaceDialog.setSelectListener(new TitanWatchFaceDialog.SelectListener() {
                            @Override
                            public void selectData(int sex) {
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanDialActivity.this);
                                }
                                if (!waitingDialog.isShowing()) {
                                    waitingDialog.show();
                                }

                                LogUtils.e(TAG, "开始设置当前表盘");
                                EABleManager.getInstance().changeTitanDial(sex, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!titanWatchFaceDialog.isShowing()) {
                        titanWatchFaceDialog.show();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (titanWatchFaceDialog != null) {
            titanWatchFaceDialog.destroyDialog();
            titanWatchFaceDialog = null;
        }

        super.onDestroy();
    }
}
